package com.ysh.controller;

import com.ysh.bean.User;
import com.ysh.service.UserService;
import com.ysh.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    private UserService service = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 统一编码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        // 获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        // 调用业务层的登录方法
        User user = service.login(username,password);

        // 判断是否查询出结果
        if (user != null){
            // 将用户名存入会话域对象当中
            req.getSession().setAttribute("username",username);
            // 响应给客户端true
            resp.getWriter().write("true");
        }else{
            resp.getWriter().write("false");
        }
    }
}
